﻿using System;

namespace Gmantis.Controls.Chart
{
    public class DataSeriesCollection : System.Collections.ObjectModel.ObservableCollection<DataSeries>
    {
        // Fields
        private ChartData _data;

        // Events
        internal event EventHandler Changed;

        // Methods
        public DataSeriesCollection()
        {
        }

        internal DataSeriesCollection(ChartData data)
        {
            this._data = data;
        }

        protected override void ClearItems()
        {
            if (this._data != null)
            {
                foreach (IDataSeriesInfo info in this)
                {
                    this._data.RemoveSeries(info);
                }
            }
            base.ClearItems();
            this.FireChanged(this, EventArgs.Empty);
        }

        private void FireChanged(object sender, EventArgs e)
        {
            if (this.Changed != null)
            {
                this.Changed(sender, e);
            }
        }

        protected override void InsertItem(int index, DataSeries item)
        {
            if (this._data != null)
            {
                this._data.AddSeries(item);
            }
            base.InsertItem(index, item);
            this.FireChanged(this, EventArgs.Empty);
        }

        protected override void RemoveItem(int index)
        {
            if (this._data != null)
            {
                this._data.RemoveSeries(base[index]);
            }
            base.RemoveItem(index);
            this.FireChanged(this, EventArgs.Empty);
        }

        protected override void SetItem(int index, DataSeries item)
        {
            if (this._data != null)
            {
                this._data.AddSeries(item);
            }
            base.SetItem(index, item);
            this.FireChanged(this, EventArgs.Empty);
        }
    }
}
